﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Taobao.common;
using System.IO;

namespace Taobao.DBServer
{
    public class DAL
    {
        #region >>GetData<<
        public bool getData(string SQL, out List<Item> plst)
        #region >><<
        {
            plst = null;

            int error = 0;
            try
            {
                DBServer server2 = new DBServer();
                DataTable dt1 = server2.ReturnDataTable(SQL, out error);
                if (error != -2)
                {
                    plst = new List<Item>();
                    if (dt1 != null && dt1.Rows.Count > 0)
                    {
                        for (int ii = 0; ii < dt1.Rows.Count; ii++)
                        {
                            Item item;
                            if (DataRow2Data(dt1.Rows[ii], out item))
                            {
                                if (item != null) { plst.Add(item); Console.Write("."); } else { Console.Write("-"); }
                            }
                            else { Console.Write("^"); }
                        }
                        Console.Write("\r\n");
                    }
                }
            }
            catch (Exception ex) { OnError(ex); }

            return plst != null;
        }
        #endregion

        public bool getData(string SQL, out List<MEMBER> plst)
        #region >><<
        {
            plst = null;

            int error = 0;
            try
            {
                DBServer server2 = new DBServer();
                DataTable dt1 = server2.ReturnDataTable(SQL, out error);
                if (error != -2)
                {
                    plst = new List<MEMBER>();
                    if (dt1 != null && dt1.Rows.Count > 0)
                    {
                        for (int ii = 0; ii < dt1.Rows.Count; ii++)
                        {
                            MEMBER item;
                            if (DataRow2Data(dt1.Rows[ii], out item))
                            {
                                if (item != null) { plst.Add(item); Console.Write("."); } else { Console.Write("-"); }
                            }
                            else { Console.Write("^"); }
                        }
                        Console.Write("\r\n");
                    }
                }
            }
            catch (Exception ex) { OnError(ex); }

            return plst != null;
        }
        #endregion

        public bool getData(string SQL, out List<SHOP> plst)
        #region >><<
        {
            plst = null;

            int error = 0;
            try
            {
                DBServer server2 = new DBServer();
                DataTable dt1 = server2.ReturnDataTable(SQL, out error);
                if (error != -2)
                {
                    plst = new List<SHOP>();
                    if (dt1 != null && dt1.Rows.Count > 0)
                    {
                        for (int ii = 0; ii < dt1.Rows.Count; ii++)
                        {
                            SHOP item;
                            if (DataRow2Data(dt1.Rows[ii], out item))
                            {
                                if (item != null) { plst.Add(item); Write("."); } else { Write("-"); }
                            }
                            else { Write("^"); }
                        }
                        Console.Write("\r\n");
                    }
                }
            }
            catch (Exception ex) { OnError(ex); }

            return plst != null;
        }
        #endregion

        public bool getData(string SQL, out SHOP item)
        #region >><<
        {
            item = null;

            int error = 0;
            try
            {
                DBServer server2 = new DBServer();
                DataTable dt1 = server2.ReturnDataTable(SQL, out error);
                if (error != -2)
                {                   
                    if (dt1 != null && dt1.Rows.Count > 0)
                    {
                        return DataRow2Data(dt1.Rows[0], out item);                       
                    }
                }
            }
            catch (Exception ex) { OnError(ex); }

            return item != null;
        }
        #endregion
        #endregion

        #region >>DataRow2Data<<
        public static bool DataRow2Data(DataRow DRow, out Item item)
        #region MyRegion
        {
            bool bi = false;
            item = null;
            try
            {
                item = new Item();
                item.ID = GetLong(DRow["ID"]);
                item.MemberID = GetLong(DRow["MemberID"]);
                item.ShopID = GetInt(DRow["ShopID"]);
                item.Name = GetString(DRow["Name"]);
                item.Note = GetString(DRow["Note"]);
                item.Price = GetDecimal(DRow["Price"]);

                item.LastUpdate = GetDateTime(DRow["LastUpdate"]);
                bi = true;
            }
            catch (Exception ex)
            {
                OnError(ex);
            }
            return bi;
        }
        #endregion

        public static bool DataRow2Data(DataRow DRow, out MEMBER item)
        #region MyRegion
        {
            bool bi = false;
            item = null;
            try
            {
                item = new MEMBER();
                item.ID = GetLong(DRow["ID"]);
                item.StoreID = GetLong(DRow["StoreID"]);
                
                item.Name = GetString(DRow["Name"]);
                item.Note = GetString(DRow["Note"]);
                item.Credit = GetInt(DRow["Credit"]);
                item.Level = GetString(DRow["Level"]);

                item.LastUpdate = GetDateTime(DRow["LastUpdate"]);
                bi = true;
            }
            catch (Exception ex)
            {
                OnError(ex);
            }
            return bi;
        }
        #endregion

        public static bool DataRow2Data(DataRow DRow, out SHOP item)
        #region MyRegion
        {
            bool bi = false;
            item = null;
            try
            {
                item = new SHOP();
                item.ID = GetLong(DRow["ID"]);
                item.MemberID = GetLong(DRow["MemberID"]);

                item.Name = GetString(DRow["Name"]);
                item.Note = GetString(DRow["Note"]);
                item.Credit = GetInt(DRow["Credit"]);
                item.Level = GetString(DRow["Level"]);

                item.LastUpdate = GetDateTime(DRow["LastUpdate"]);
                item.TimeStp = TimeStampToLong((byte[])DRow["TimeStp"]);
                bi = true;
            }
            catch (Exception ex)
            {
                OnError(ex);
            }
            return bi;
        }
        #endregion
        #endregion

        #region >>Tools<<
        public static long TimeStampToLong(byte[] b)
        {
            try
            {
                if (b.Count() != 8)
                    return 0;
                long l;
                l = long.Parse(b[0].ToString("x") + b[1].ToString("x") + b[2].ToString("x") + b[3].ToString("x") + b[4].ToString("x") + b[5].ToString("x") + b[6].ToString("x") + b[7].ToString("x"), System.Globalization.NumberStyles.AllowHexSpecifier);
                return l;
            }
            catch (Exception ex)
            {
                OnError(ex);
            }
            return 0;
        }

        public static string Long2TimeStamp(long value)
        {
            string ss = value.ToString("X16");
            return ss;
            //switch (ss.Length)
            //{
            //    case 2: return "00000000000000" + ss;
            //    case 4: return "000000000000" + ss;
            //    case 6: return "0000000000" + ss;
            //    case 8: return "00000000" + ss;
            //    case 10: return "000000" + ss;
            //    case 12: return "0000" + ss;
            //    case 14: return "00" + ss;
            //    case 16: return ss;

            //    default: return "0000000000000000";
            //}
        }

        public static int GetInt(object o)
        {
            if (o == null || o == DBNull.Value)
                return 0;
            else
                return Convert.ToInt32(o);
        }

        public static long GetLong(object o)
        {
            if (o == null || o == DBNull.Value)
                return 0;
            else
                return Convert.ToInt64(o);
        }

        public static decimal GetDecimal(object o)
        {
            if (o == null || o == DBNull.Value)
                return 0;
            else
                return Convert.ToDecimal(o);
        }

        public static string GetString(object o)
        {
            if (o == null || o == DBNull.Value)
                return "";
            else
                return o.ToString();
        }

        public static DateTime GetDateTime(object o)
        {
            if (o == null || o == DBNull.Value)
                return DateTime.MinValue;
            else
                return Convert.ToDateTime(o);
        }

        public static string ReturnYorN(int num)
        {
            string result = string.Empty;
            result = num > 0 ? "Y" : "N";
            return result;
        }
        #endregion

        #region >>WriteSqlLog<<

        private static bool _isWriteSqlLog = true;
        public static bool isWriteSqlLog { get { return _isWriteSqlLog; } set { _isWriteSqlLog = value; } }
        public void WriteSqlLog(string className, string sql)
        {
            DateTime now = DateTime.Now;
            string path = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "SP";
            string fileName = Path.DirectorySeparatorChar + className + now.ToString("yyyy-MM-dd") + ".txt";

            if (!Directory.Exists(path)) { Directory.CreateDirectory(path); }

            FileStream fs = null;
            StreamWriter sw = null;
            try
            {
                fs = File.Open(path + Path.DirectorySeparatorChar + fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
                fs.Seek(0, SeekOrigin.End);
                sw = new StreamWriter(fs, System.Text.Encoding.Unicode);
                sw.WriteLine(now.ToLongTimeString() + " " + sql);
            }
            finally
            {
                if (sw != null)
                    sw.Close();
                if (fs != null)
                    fs.Close();
            }
        }
        #endregion

        #region >>ConsoleSay<<
        public static string Nowstr { get { return DateTime.Now.ToString("<HH:mm:ss>"); } }
        protected static void WriteLine(string msg)
        {
            Console.WriteLine(msg);
        }

        public static void WriteLine(string n2, string msg)
        {
            Console.WriteLine("[" + n2 + "]" + msg);
        }

        public static void WriteLine(string N1, string n2, string msg)
        {
            Console.WriteLine("[" + N1 + ":" + n2 + "]" + msg);
        }

        public static void OnError(Exception ex)
        {
            //OUtl.MSGCenter.OnError(ex);
        }

        protected static void Write(string msg)
        {
            Console.Write(msg);
        }
        #endregion
    }
}
